package com.zyd.multilingual;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.zyd.multilingual.model.LanguageBean;

import java.util.ArrayList;
import java.util.List;

public class SelectLanguageActivity extends BaseActivity implements View.OnClickListener {

    private TextView tvChinese;
    private TextView tvEnglish;
    private TextView tvDeutsch;
    private List<LanguageBean> languageList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_language);
        initView();
        initData();
    }

    private void initView() {
        tvChinese = findViewById(R.id.asl_tv_chinese);
        tvEnglish = findViewById(R.id.asl_tv_english);
        tvDeutsch = findViewById(R.id.asl_tv_deutsch);
        tvChinese.setOnClickListener(this);
        tvEnglish.setOnClickListener(this);
        tvDeutsch.setOnClickListener(this);
    }

    private void initData() {
        languageList = new ArrayList<>();
        languageList.add(new LanguageBean(0, "简体中文", "zh", false));
        languageList.add(new LanguageBean(1, "English", "en", false));
        languageList.add(new LanguageBean(2, "德语", "de", false));
    }

    @Override
    public void onClick(View v) {

        SharedPreferences preferences = getSharedPreferences("language", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        switch (v.getId()){
            case R.id.asl_tv_chinese:
                editor.putString("language", languageList.get(0).getShortName());
                break;
            case R.id.asl_tv_english:
                editor.putString("language", languageList.get(1).getShortName());
                break;
            case R.id.asl_tv_deutsch:
                editor.putString("language", languageList.get(2).getShortName());
                break;
        }
        editor.apply();
        Intent intent = new Intent(this, LanguageMainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
}